<?php
namespace Tbf\Bundle\DatabaseBundle\DataSet;
use Symfony\Component\Config\FileLocator;
use Tbf\Error\InvalidArgumentException;
/**
 * 加载一个数据库的DataSet
 */
class Loader
{
    protected $loader ;
    protected $data_set ;
    public function __construct(array $path_list=array())
    {
        $locator = new FileLocator($path_list);
        $this->loader = new PhpFileLoader($locator);
        $this->data_set = new DbDataSet;
    }
    public function append($path)
    {
        $this_data_set = $this->getDataSetFromPath($path);
        $this->data_set->append($this_data_set);

        return $this;
    }
    public function set($path)
    {
        $this_data_set = $this->getDataSetFromPath($path);
        $this->data_set->set($this_data_set);

        return $this;
    }
    public function getDataSet()
    {
        return $this->data_set;
    }
    public function toArray()
    {
        return $this->data_set->toArray();
    }
    public function getDataSetFromPath($path)
    {
        if (is_array($path)) {
            return  new DbDataSet($path);
        } elseif (is_string($path)) {
            return  $this->loader->load($path);
        } else {
            throw new InvalidArgumentException(
                'invaild type of $path');
        }
    }
}//class
